class WxRequestUtils {
  constructor(baseUrl) {
      this.baseUrl = baseUrl;
  }

  request(options) {
      const {
          url,
          method = 'GET',
          data = {},
          header = {
              'content-type': 'application/json'
          }
      } = options;

      return new Promise((resolve, reject) => {
          wx.request({
              url: this.baseUrl + url,
              method,
              data,
              header,
              success: (res) => {
                  if (res.statusCode >= 200 && res.statusCode < 300) {
                      resolve(res.data);
                  } else {
                      reject(new Error(`请求失败，状态码: ${res.statusCode}`));
                  }
              },
              fail: (err) => {
                  reject(new Error(`请求出错: ${err.errMsg}`));
              }
          });
      });
  }

  get(url, data = {}) {
      return this.request({ url, method: 'GET', data });
  }

  post(url, data = {}) {
      return this.request({ url, method: 'POST', data });
  }

  put(url, data = {}) {
      return this.request({ url, method: 'PUT', data });
  }

  delete(url, data = {}) {
      return this.request({ url, method: 'DELETE', data });
  }
}

export default WxRequestUtils;
  